package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ReservationMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Reservation;
import com.itheima.pojo.vo.ReservationVo;
import com.itheima.service.ReservationService;
import com.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Override
    public PageBean<ReservationVo> list(Integer pageNum, Integer pageSize, String name, String username,Integer status) {
        PageBean<ReservationVo> pageBean=new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<ReservationVo> voList=reservationMapper.list(name,username,status);
        Page<ReservationVo> page=(Page<ReservationVo>) voList;
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());
        return pageBean;
    }

    @Override
    public void add(Reservation reservation) {
        reservation.setStatus(0);
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer userId=(Integer) map.get("id");
        reservation.setUserId(userId);
        reservation.setReservationTime(LocalDateTime.now());
        reservationMapper.add(reservation);
    }

    @Override
    public void updateStatus(Integer id,Integer status) {
        reservationMapper.update(id,status);
    }

    @Override
    public void delete(Integer id) {
        reservationMapper.delete(id);
    }
}
